// component/secretMsg/index.js
let modifyClick = false
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    school: '',
    company: '',
    emotion: '',
    date: '',
    region: [],
    chatWay: ''
  },

  created: function(){
    this.getRightData()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getRightData(){
      wx.cloud.callFunction({
        name: 'getRightInfo'
      }).then(res => {
        if(res.result){
          this.setData({
            school: res.result.school,
            company: res.result.company,
            emotion: res.result.emotion,
            date: res.result.birthday,
            region: res.result.address,
            chatWay: res.result.chatway
          })
        }
      })
    },
    getSchool(e) {
      this.setData({
        school: e.detail.value
      })
    },
    getCompany(e){
      this.setData({
        company: e.detail.value
      })
    },
    getEmotion(e) {
      this.setData({
        emotion: e.detail.value
      })
    },
    DateChange(e) {
      this.setData({
        date: e.detail.value
      })
    },
    RegionChange: function (e) {
      this.setData({
        region: e.detail.value
      })
    },
    getChatWay(e) {
      this.setData({
        chatWay: e.detail.value
      })
    },
    comfireModified(){
      if (modifyClick){
        return
      }else{
        modifyClick = true
      }
      if (this.data.chatWay == '') 
        return wx.showToast({
          title: '交流方式不能为空！',
          icon: 'none',
          duration: 2000
        })
      this.setData({
        loading: true
      })
      wx.cloud.callFunction({
        name: 'modifyRightInfo',
        data: {
          school: this.data.school,
          company: this.data.company,
          emotion: this.data.emotion,
          birthday: this.data.date,
          address: this.data.region,
          chatway: this.data.chatWay
        }
      }).then(res => {
        this.setData({
          loading: false
        })
        this.triggerEvent('getInfoState', true)
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      })
    }
  }
})
